package com.example.administrator.printoutindemo;

import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.IdRes;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v7.app.AppCompatActivity;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.example.administrator.printoutindemo.fragment.InFragment;
import com.example.administrator.printoutindemo.fragment.OutFragment;
import com.example.administrator.printoutindemo.fragment.RuKuSuccessFragment;

import me.jessyan.armscomponent.commonsdk.core.RouterHub;

@Route(path = RouterHub.APP_MAINACTIVITY)
public class MainActivity extends AppCompatActivity {
    RadioGroup navgroup;
    private Fragment FragmentOne, FragmentTwo, FragmentThree;
    private Fragment mFragment;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        FragmentOne = new InFragment();
        FragmentTwo = new OutFragment();
        FragmentThree = new RuKuSuccessFragment();
        navgroup = (RadioGroup) findViewById(R.id.navgroup);
        FragmentManager fragmentManager = getSupportFragmentManager();
        fragmentManager.beginTransaction().add(R.id.main_fragment, FragmentOne).commit();

        mFragment = FragmentOne;
        navgroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup radioGroup, @IdRes int i) {
                switch (i) {
                    case R.id.rb_in:
                        switchFragmet(FragmentOne);
                        break;
                    case R.id.rb_out:
                        switchFragmet(FragmentTwo);
                        break;
                    case R.id.rb_rkcgjl:
                        switchFragmet(FragmentThree);
                        break;
                }
            }
        });
    }

    public void switchFragmet(Fragment fragment) {
        if (mFragment != fragment) {
            if (!fragment.isAdded()) {
                getSupportFragmentManager().beginTransaction().hide(mFragment).add(R.id.main_fragment, fragment).commit();
            } else {
                getSupportFragmentManager().beginTransaction().hide(mFragment).show(fragment).commit();
            }
            mFragment = fragment;
        }
    }
}
